Welcome to GeeKee CeeBee's Page: House of Mechatronics Projects & Lessons.
Contact Email: Ceebee1108@gmail.com
Follow me on Youtube
__________________________________________________________________________________________________________________________________
% By GeeKee CeeBee https://www.youtube.com/watch?v=wUFOzGMHues&feature=youtu.be
% Solve Differential Using Finite Difference Method in Discrete Time
% This example looks at Spring Mass Damper System
clear all; close all; clc
K=1000; % Spring Constant
M= 10; % Mass
C= 100; % Damping Coefficient
dt=0.001; % Time step
t=[0:dt:5]; % Time range
y= 0.1 * heaviside(t-1); % Step function
x(1)=0; % Initial Conditions
x(2)=0;
A1= (C/dt)+(M/dt^2)+K;
A2= (-2*M/dt^2)-(C/dt);
A3=(M/dt^2) ;
A4= (C/dt)+K;
A5= (-C/dt);
for k=3:length(t)
x(k)= (1/A1)*(A4*y(k)+A5*y(k-1)-A2*x(k-1)-A3*x(k-2));
end
figure; plot(t,x,t,y,'linewidth',2);
xlabel('t (s)');
ylabel('X (m)');
legend('x(t)', 'y(t)');
title('Displacement vs Time');